package com.eastelsoft.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gitee.myclouds.toolbox.wrap.Dto;
import com.gitee.myclouds.toolbox.wrap.impl.HashDto;

@RestController
@RequestMapping(value="jdbc")
public class JdbcController {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@RequestMapping(value="query", method=RequestMethod.GET)
	public String findById(HttpServletRequest request) {
		String id = request.getParameter("id");
		
		String sql = "SELECT * FROM my_user where id = ?";
//		List<Dto> list = jdbcTemplate.queryForList(sql, new Object[]{id}, Dto.class);
//		System.out.println("name -----------> "+list.get(0));
//		
//		return list.get(0).getString("account");
		Map<String, Object> dto = jdbcTemplate.queryForMap(sql, new Object[]{id});
		System.out.println("name -----------> "+String.valueOf(dto.get("account")));
		
		return String.valueOf(dto.get("account"));
	}
	
	@RequestMapping(value="queryAll", method=RequestMethod.GET)
	public String findAll(HttpServletRequest request) {
		String sql = "SELECT * FROM my_user";
//		List<Dto> list = jdbcTemplate.queryForList(sql, new Object[]{id}, Dto.class);
//		System.out.println("name -----------> "+list.get(0));
//		
//		return list.get(0).getString("account");
		List<Map<String, Object> > list = jdbcTemplate.queryForList(sql);
		
		return JSON.toJSONString(list);
	}
	
}
